/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.googlecode.microanalyst.designer.edit;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;

import com.googlecode.microanalyst.designer.figures.FigureFactory;
import com.googlecode.microanalyst.designer.model.AnalysisConnection;
import com.googlecode.microanalyst.designer.model.AnalysisElement;
import com.googlecode.microanalyst.designer.model.commands.ConnectionCommand;

public class AnalysisNodeEditPolicy extends
		org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy {

	protected Connection createDummyConnection(Request req) {
		PolylineConnection conn = FigureFactory.createNewConnection(null);
		return conn;
	}

	protected Command getConnectionCompleteCommand(
			CreateConnectionRequest request) {
		ConnectionCommand command = (ConnectionCommand) request
				.getStartCommand();
		command.setTarget(getAnalysisElement());
		ConnectionAnchor ctor = getAnalysisEditPart()
				.getTargetConnectionAnchor(request);
		if (ctor == null)
			return null;
		command.setTargetTerminal(getAnalysisEditPart()
				.mapConnectionAnchorToTerminal(ctor));
		return command;
	}

	protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
		ConnectionCommand command = new ConnectionCommand();
		command.setConnection(new AnalysisConnection());
		command.setSource(getAnalysisElement());
		ConnectionAnchor ctor = getAnalysisEditPart()
				.getSourceConnectionAnchor(request);
		command.setSourceTerminal(getAnalysisEditPart()
				.mapConnectionAnchorToTerminal(ctor));
		request.setStartCommand(command);
		return command;
	}

	/**
	 * Feedback should be added to the scaled feedback layer.
	 * 
	 * @see org.eclipse.gef.editpolicies.GraphicalEditPolicy#getFeedbackLayer()
	 */
	protected IFigure getFeedbackLayer() {
		/*
		 * Fix for Bug# 66590 Feedback needs to be added to the scaled feedback
		 * layer
		 */
		return getLayer(LayerConstants.SCALED_FEEDBACK_LAYER);
	}

	protected AnalysisEditPart getAnalysisEditPart() {
		return (AnalysisEditPart) getHost();
	}

	protected AnalysisElement getAnalysisElement() {
		return (AnalysisElement) getHost().getModel();
	}

	protected Command getReconnectTargetCommand(ReconnectRequest request) {

		ConnectionCommand cmd = new ConnectionCommand();
		AnalysisConnection connection = (AnalysisConnection) request.getConnectionEditPart().getModel();
		cmd.setConnection(connection);

		ConnectionAnchor ctor = getAnalysisEditPart()
				.getTargetConnectionAnchor(request);
		cmd.setTarget(getAnalysisElement());
		cmd.setTargetTerminal(getAnalysisEditPart()
				.mapConnectionAnchorToTerminal(ctor));
		return cmd;
	}

	protected Command getReconnectSourceCommand(ReconnectRequest request) {
		ConnectionCommand cmd = new ConnectionCommand();
		cmd.setConnection((AnalysisConnection) request.getConnectionEditPart().getModel());

		ConnectionAnchor ctor = getAnalysisEditPart()
				.getSourceConnectionAnchor(request);
		cmd.setSource(getAnalysisElement());
		cmd.setSourceTerminal(getAnalysisEditPart()
				.mapConnectionAnchorToTerminal(ctor));
		return cmd;
	}

}
